package com.mall.b2c.hessian.impl;

import com.google.common.collect.Sets;
import com.mall.b2c.bean.IntergralShopClassBean;
import com.mall.b2c.bean.IntergralShopOrderBean;
import com.mall.b2c.bean.IntergralShopProductBean;
import com.mall.b2c.dao.CartItemDao;
import com.mall.b2c.dao.MemberDao;
import com.mall.b2c.dao.ShopCartItemDao;
import com.mall.b2c.domain.intergral.ShopCartItemDomain;
import com.mall.b2c.domain.intergral.ShopClassDomain;
import com.mall.b2c.domain.intergral.ShopProductDomain;
import com.mall.b2c.domain.member.MemberDomain;
import com.mall.b2c.domain.order.CartItemDomain;
import com.mall.b2c.domain.order.OrderItemDomain;
import com.mall.b2c.hessian.CommentHessian;
import com.mall.b2c.hessian.IntergralHessian;
import com.mall.b2c.service.*;
import com.mall.common.bean.Message;
import com.mall.common.exception.NoticeException;
import com.mall.common.hessian.impl.BaseHessianImpl;
import com.mall.common.hibernate.Finder;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.persistence.LockModeType;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by chen on 16/12/19.
 */
@Service("intergralHessian")
public class IntergralHessianImpl  extends BaseHessianImpl implements IntergralHessian {
    @Resource
    IntergralShopProductService intergralShopProductService;
    @Resource
    IntergralShopClassService intergralShopClassService;
    @Resource
    IntergralOrderService intergralOrderService;
    @Resource private MemberDao memberDao;
    @Resource
    IntergralService intergralService;
    @Resource private ShopCartItemDao shopCartItemDao;
    @Resource
    IntergralLogService intergralLogService;
    @Override
    public String getShopClassList(){
        List<ShopClassDomain>classDomains = intergralShopClassService.getAll();
        List<IntergralShopClassBean> shoporderlist = new  ArrayList<IntergralShopClassBean>();
        for(ShopClassDomain domain:classDomains){
          shoporderlist.add(new IntergralShopClassBean(domain));
        }
        return listToJson(shoporderlist);
    }

    @Override
    public String getShopProductList(Long classId,String orderBy)
    {
        List  <IntergralShopProductBean> list = intergralShopProductService.getShoproductList(classId,orderBy);
        return listToJson(list);
    }

    @Override
    public String getOrderlist(Long userid,String orderby) {
        List  <IntergralShopOrderBean> list = intergralOrderService.getOrderList(userid,orderby);

        return listToJson(list);

    }
    @Override
    public String getProductDetail(Long id){
        if(id==null){
            throw new NoticeException("该商品不存在");
        }
        return objToJson(new IntergralShopProductBean(intergralShopProductService.find(id)));

    }
    @Override
    public String exchange(String name,String phone,String address,int num,Long productId,Long MemberId){

        IntergralShopOrderBean bean = new IntergralShopOrderBean();
        bean.setAddress(address);
        bean.setName(name);
        bean.setNum(num);
        bean.setPhone(phone);
        bean.setMemberId(MemberId);
        bean.setProductId(productId);

        Message message = intergralOrderService.exchange(bean);

        if (message.getType().toString().equals("SUCCESS")) {
            return "1";
        } else {
            return "0";
        }


    }
    @Override
    public String getIntergralByUserId(Long userid){
        return intergralService.getByMemberId(userid).getNum()+"";
    }

    @Override
    public String getIntergralLogList(Long userid, Integer pageNumber){
            return objToJson(intergralLogService.findByUid(userid, pageNumber, 10));
    }

    @Override
    public void createOrder(Long currStoreId, Long memberId, Long[] cartItemIds, String name, String phone, String address,Long store) {
         intergralOrderService.createOrder(currStoreId, memberId,cartItemIds, name, phone, address,store);
    }
}
